import {
	CmdMemory,
	DissassembleScroll,
	CmdDissassemble
} from "./structs.slint";
import { UiIo } from "global.slint";
import { TestData } from "test-data.slint";
import { ScrollView } from "scroll_view.slint";
import { HorizontalBox, LineEdit, Button, TextEdit, ListView, VerticalBox } from "std-widgets.slint";

export component AssembleView {
	property <length> le_width: 120px;

	focus_scope := FocusScope {
		x: 0;
		width: 0;  // Do not react on clicks
		enabled: true;

		key_pressed(event) => {
			if (event.text == Key.UpArrow) {
				UiIo.assemble-scroll(true, list_view.height, DissassembleScroll.Line);
				return accept;
			} else if (event.text == Key.DownArrow) {
				UiIo.assemble-scroll(false, list_view.height, DissassembleScroll.Line);
				return accept;
			} else if (event.text == Key.PageUp) {
				UiIo.assemble-scroll(true, list_view.height, DissassembleScroll.Page);
				return accept;
			} else if (event.text == Key.PageDown) {
				UiIo.assemble-scroll(false, list_view.height, DissassembleScroll.Page);
				return accept;
			}

			reject
		}
	}
	
	Rectangle {
		border-color: gray;
		border-width: 1px;
		border-radius: 3px;
		VerticalLayout {
			HorizontalBox {
				padding: 3px;
				flash_start_address:= LineEdit {
					text: UiIo.flash_start_address;
					placeholder-text: "Start address";
					width: root.le_width;
				}
				flash_size:= LineEdit {
					text: UiIo.flash_size;
					placeholder-text: "Size";
					width: root.le_width;
				}
				Button {
					text: "DisAsm";
					clicked => { UiIo.probe-disassemble(CmdDissassemble.DissasmALL, flash_start_address.text, flash_size.text); }
				}
				Button {
					text: "Export";
					clicked => { UiIo.probe-disassemble(CmdDissassemble.Export, "", ""); }
				}
				Rectangle {}
			}
			list_view := ScrollView {
				VerticalLayout {
					for line in UiIo.dissassemble_line : HorizontalLayout {
						Rectangle {
							border-width: focus_scope.has_focus ? 3px : 1px;
							border-color: white;
							border-radius: 10px;
							height: UiIo.assemble_line_height;
							width: UiIo.assemble_line_height;
							background: line.breakpoint ? red : touch.has-hover ? #950c1c : transparent;
							touch := TouchArea {
								mouse-cursor: pointer;
								clicked => {
									line.breakpoint =!line.breakpoint;
									UiIo.probe-set-breakpoint(line.breakpoint, line.address);
								}
								pointer_event(event) => {
									if (event.button == PointerEventButton.right && event.kind == PointerEventKind.down) {
										UiIo.probe-set-breakpoint(false, -1);
									}
								}
							}
						}
						Rectangle { width: 6px; }
						Rectangle{
							background: line.address ==  UiIo.PC ? gray : transparent;
							HorizontalLayout {
								Text {
									vertical-alignment: center;
									text: line.assemble;
								}
							}
							touch_0 := TouchArea {
								clicked => {
									focus_scope.focus();
								}
								scroll-event(event) => {
									//debug(event);
									if (event.delta-y > 0 ) {//up
										UiIo.assemble-scroll(true, list_view.height, DissassembleScroll.Scroll);
									}
									if (event.delta-y < 0) {//bottom
										UiIo.assemble-scroll(false, list_view.height, DissassembleScroll.Scroll);
									}
									return EventResult.reject;
								}
							}
						}
					}
				}
			}
			if (UiIo.dissassemble_line.length == 0 ) : Rectangle {}
		}
	}
}
